<?php
/**
 * Display the list of Medical Centers where the current
 * user is attached as a partner
 *
 * @author Xavier VILAIN
 * @package Nawa\MedicalCenters
 * @license
 */

use Fox\Dao\SqlFragment;
use Nawa\MedicalStudy\MedicalCenter\Monitoring;
use Fox\Generator;
use Fox\Html\Block;
use Fox\Grid;
use Nawa\Messages;

$monitorings = Fox\Dao::finder(Monitoring::className())
					->where('status != ?',array(Monitoring::STATUS_VALIDATED))
					->orderBy('udate','DESC')
					->follow(Monitoring::LINK_PARTNERS)
						->where('customerid = ?',array(User::current()->contactId()))
						->find();
$grid = new Grid('gMyMonitorings');
$grid->fieldcat(array('ms_reference',array('monitoring_reference','reference'),'monitoring_date','uname','udate'));
$grid->fieldcat()->switchToObjectMode();
$grid->fieldcat()->field('udate')->displayTime();
$grid->fieldcat()->setGenerators(array(
	'reference' => Generator::call('label'),
	'ms_reference' => Generator::useFunction(function ($data){
		return $data->medicalCenterPartner()->medicalStudy()->reference;		
	})
));
$grid->fieldcat()
	->field('ms_reference')
		->label(Msg(Nawa\Messages::BUNDLE,Nawa\Messages::LABEL_STUDY));

$grid->onClick(Generator::location(Generator::url('medicalstudies','medicalcenters','monitoring',
array(	'msid' => Generator::useAttribute('msid'),
		'mcid' => Generator::useAttribute('mcid'),
		'monitoringid' => Generator::useAttribute('monitoringid')))));
$grid->initialize($monitorings,true);
if(Xview::instance()->ajax())
{
	echo $grid->display();
}
else
{
	$block = new Block('bMedicalCenters',Msg(Messages::BUNDLE,Messages::LABEL_MY_MONITORINGS));
	$block->collapsed(false);
	$block->add($grid);
	return $block;
}